package com.bungarus.smart.controller;

import com.bungarus.cluster.ClusterManager;
import com.bungarus.smart.busi.AccessContainer;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.http.CacheControl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import com.bungarus.smart.busi.Scanner;

@RestController
@RequestMapping("/api/smart/v1.0")
public class RouteController extends BungarusController{

    Logger logger = Logger.getLogger(RouteController.class);
    public static final String CLUSTER_TAG_ACCESS = "access";
    private ClusterManager clusterManager = new ClusterManager(CLUSTER_TAG_ACCESS);

    private static ApplicationContext applicationContext;

    public ClusterManager getClusterManager() {
        return clusterManager;
    }

    @GetMapping("/routes")
    public ResponseEntity<List<String>> doGetAccessNodeIps(@RequestParam(name = "seed", required = false) String seed, @RequestParam(name = "seed", required = false) String sig) {

        List<String> nodeIpList = new ArrayList<>();
        int clusterId = getAccessClusterByRoute();
        if(clusterId >= 0){
            try {
                nodeIpList = AccessContainer.getInstance().getAccessList();
            } catch (Exception e){
                e.printStackTrace();
            }
        }
        else{
            logger.warn("No proper access nodes found!");
        }

        return ResponseEntity.ok()
                .cacheControl(CacheControl.noCache())
                .header("Pragma", "no-cache")
                .body(nodeIpList);
    }

    /**
     * get proper clusterID by some strategy
     * @return clusterID
     */
    private int getAccessClusterByRoute(){
        //TODO: filter the clusters by strategy
        return 0;
    }

}
